package vip.bblog.oauth2.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import vip.bblog.oauth2.dao.OauthClientDetailsMapper;
import vip.bblog.oauth2.entity.OauthClientDetails;
import vip.bblog.oauth2.service.OauthClientDetailsService;
import vip.bblog.common.utils.UUIDUtils;
import vip.bblog.common.utils.BaseResult;

import java.time.LocalDateTime;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yuyong
 * @since 2019-05-15
 */
@Service
public class OauthClientDetailsServiceImpl extends ServiceImpl<OauthClientDetailsMapper, OauthClientDetails> implements OauthClientDetailsService {

    @Autowired
    private OauthClientDetailsMapper oauthClientDetailsMapper;

    @Autowired
    private PasswordEncoder passwordEncoder;

    /**
     * 增加客户端
     *
     * @param clientDetails
     * @return
     */
    @Override
    public BaseResult<Object> insert(OauthClientDetails clientDetails) {
        clientDetails.setClientId(UUIDUtils.getUUID());
        clientDetails.setClientSecret(passwordEncoder.encode(UUIDUtils.getUUID()));
        clientDetails.setCreateTime(LocalDateTime.now());
        oauthClientDetailsMapper.insert(clientDetails);
        return new BaseResult<>(clientDetails);
    }

    /**
     * 根据clientId删除
     *
     * @param clientId
     * @return
     */
    @Override
    public BaseResult<Object> deleteByClientId(String clientId) {
        oauthClientDetailsMapper.deleteById(clientId);
        return new BaseResult<>();
    }

    /**
     * 根据clientId更新信息
     *
     * @param clientDetails
     * @return
     */
    @Override
    public BaseResult<Object> updateByClientId(OauthClientDetails clientDetails) {
        oauthClientDetailsMapper.updateById(clientDetails);
        return new BaseResult<>(clientDetails);
    }

    /**
     * 模糊分页查询
     *
     * @param page
     * @param clientDetails
     * @return
     */
    @Override
    public BaseResult<Object> selectPageByExample(Page<OauthClientDetails> page, OauthClientDetails clientDetails) {
        QueryWrapper<OauthClientDetails> queryWrapper = new QueryWrapper<>();
        oauthClientDetailsMapper.selectPage(page, queryWrapper);
        return null;
    }
}
